package com.relytosoft.mizar.analyze.system.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.system.api.entity.AyAlarm;
import com.relytosoft.mizar.analyze.system.api.pojo.dto.AyAlarmDto;
import com.relytosoft.mizar.analyze.system.services.service.AyAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/24 17:31
 */
@Api(tags = {"布防告警结果查看"})
@RestController
@RequestMapping("/system/alarm")
public class AyAlarmServiceController {

    @Resource
    private AyAlarmService ayAlarmService;

    @ApiOperation("添加告警")
    @PostMapping("/add")
    public ResponseResult<Void> addAyAlarm(@RequestBody AyAlarmDto ayAlarmDto) {
        AyAlarm ayAlarm = BeanCopyUtils.copy(ayAlarmDto, AyAlarm.class);
        boolean alarm = ayAlarmService.addAyAlarm(ayAlarm);
        if (alarm) {
            return ResponseResult.ok("添加告警成功");
        } else {
            return ResponseResult.fail("添加告警失败");
        }
    }

    @ApiOperation("告警信息查询")
    @PostMapping("/list")
    public ResponseResult<Map<String, Object>> list(AyAlarmDto ayAlarmDto, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyAlarm ayAlarm = BeanCopyUtils.copy(ayAlarmDto, AyAlarm.class);
        IPage<AyAlarm> page = ayAlarmService.selectAyAlarmPageList(pageQuery, ayAlarm);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }
}
